#include <math.h>

static const hibbard_step[] = {
	1, 3, 7, 15, 31, 63, 127, 255, 511, 1023, 
	2047, 4095, 8191, 16383, 32767, 65535, 131071, 262143, 524288
};
int shell_sort(int *data, int data_lenth, int flag)
{
	if (data == 0 || data_lenth <= 0)
	{
		return -1;
	}	
	

	int step;
	int i,j;
	int temp;


	if (flag == 0)
	{
	
		for (step = data_lenth/2; step > 0; step /= 2)
		{
			for (i = step; i < data_lenth; i++)
			{
				temp = data[i];
				for (j = i-step; j >= 0 && data[j] > temp; j -= step)
				{
					data[j + step] = data[j];
				}		
				if (j != i-step)
				{
					data[j + step] = temp;
				}
		
			}
		}
	
	}
	else
	{
		int sumtimes = (int)(log(data_lenth+1)/log(2));
		int z;
	
		for (z = 1; z <= sumtimes; z++)
		{
			step = hibbard_step[sumtimes - z];
			for (i = step; i < data_lenth; i++)
			{
				temp = data[i];
				for (j = i-step; j >= 0 && data[j] > temp; j -= step)
				{
					data[j + step] = data[j];
				}		
				if (j != i-step)
				{
					data[j + step] = temp;
				}
		
			}	
		
		}
	}

	return 0;
}
